package com.pignest.hotel.service;

import java.util.Date;
import java.util.List;

import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.OperateState;
import com.pignest.hotel.util.DateUtil;

@SuppressWarnings("unchecked")
public class AuditService extends BaseService {

	/**
	 * 审记功能
	 * */
	private ReceptionService receptionServices;
	public String currentDate=null;//格式为yyyy-MM-dd
	
	/**
	 * 自动更新入住所产生的当天房费
	 * 每晚12点自动执行
	 * 这种做法有一个问题：
	 * 如果服务没有起或者当到12点，由于停电或者其它原因造成服务
	 * 没法启动，12点之前的入住就不会产生费用！！因此必须有一功能作
	 * 目前是通过前台结账人员人为查看是否有产生当天房，如果没有则可操作增加房费
	 * 手动触发该功能
	 * 业务描述：
	 *  1、查找未退房的记录(正在入住的记录)
	 *  2、查找当前是当天是否已产生房费了，不包括加收房费
	 *     类型：type = '房费'
	 *     TbAccounts
	 *     LivingSpecifics入住详细信息表中
	 *  3、产生当天房费
	 *  4、产生当天房费的详细信息
	 * **/
	public void execCalculateCurrentAccounts() {
		currentDate = DateUtil.currentDateToString("yyyy-MM-dd");
		List<Living> livings = currentLiving();
		for (Living l : livings) {
			boolean isExist = isExistCurrentAccounts(l);
			if (isExist) {
				addAccounts(l);
				l = receptionServices.updateCalculate(l);
				addSpecifics(l);
				update(l);
			}
		}
	}

	public List<Living> currentLiving()
	{
		StringBuffer hql = new StringBuffer();
		hql.append("from Living b where b.liveState='").append(LivingState.LIVING.name()).append("'");
		hql.append(" or b.liveState='").append(LivingState.PARTCHECKOUT.name()).append("'");
		List <Living> livings=this.find(hql.toString());
		return livings;
	}
	/**
	 * 是否产生当天费用
	 * 日期加类型决定
	 * ***/
	public boolean isExistCurrentAccounts(Living l)
	{
		StringBuffer hql = new StringBuffer();
		hql.append(" from TbAccounts b where b.living.id='").append(l.getId()).append("'");
		hql.append(" and b.type='").append(InComeItemType.FF.name()).append("'");
		if(currentDate==null)
			currentDate= DateUtil.currentDateToString("yyyy-MM-dd");
		hql.append(" and b.opdate").append("='").append(currentDate).append("'");
		List list=this.find(hql.toString());
		if(list!=null&&list.size()==0)
		    return true;
		else
	     return false;
	}
	public void addAccounts(Living l)
	{
		TbAccounts accounts=new TbAccounts();
		accounts.setLiving(l);
		accounts.setType(InComeItemType.FF.name());
		accounts.setPrice(-l.getAdjustPrice());
		accounts.setCounts(1);
		accounts.setEndstate("否");
		accounts.setName(InComeItemType.FF.getValue());
		accounts.setOptime(new Date());
		accounts.setOpdate(currentDate);
		accounts.setOpUser("[系统]");
		accounts.setPrice2(l.getAdjustPrice());
		accounts.setRemark(currentDate+":"+InComeItemType.FF.getValue());
		this.insert(accounts);
	
	}
	public void addSpecifics(Living l)
	{
		
		LivingSpecifics spec = new LivingSpecifics();
		spec.setRoom(l.getRoom().getName());
		spec.setGrantDate(l.getGrantDate());
		spec.setLeaveDate(l.getExpectLeaveDate());
		spec.setLiving(l);
		spec.setOperateDate(new Date());
		spec.setOperateType(OperateState.ROOMPRICE.name());
		spec.setPay(l.getStorage());
		spec.setRoomPrice(l.getAdjustPrice());
		spec.setRemark(OperateState.ROOMPRICE.getValue());
		this.insert(spec);
	}
	public String selfCheckOutCalculate(Living l)
	{
		if(l==null)
			return "无效的入住";
		Date liveTime =l.getLiveTime();
		String sCurTime =DateUtil.currentDateToString(DateUtil.DEFAULT_DATE_FORMAT);
		String sLiveTime =DateUtil.toString(liveTime, DateUtil.DEFAULT_DATE_FORMAT);
		Double days =DateUtil.parseLiveDays(sLiveTime, sCurTime);
		
		return null;
	}
	public void setReceptionServices(ReceptionService receptionServices) {
		this.receptionServices = receptionServices;
	}

}
